﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.common;
using rock.DAL;
using rock.common;

namespace rock.UI.contract
{
    public partial class FormSelectContract : Form
    {
        private int CURRENT_PAGE = 1;
        DalRENT_CONTRACT dal = new DalRENT_CONTRACT();
        public FormSelectContract()
        {
            InitializeComponent();

            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;

            this.userControlCommonListHeader1.searchClick = searchClick;

            this.userControlCommonListPage1.pageClick = refreshData;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
            this.Icon = global::rock.Properties.Resources.house;

        }
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }

            this.refreshData(this.CURRENT_PAGE);
        }

        //刷新数据
        public void refreshData(int goPage)
        {


            CURRENT_PAGE = goPage;
            String search = this.userControlCommonListHeader1.textBox1.Text;
            StringBuilder where = new StringBuilder(
                 "(" +
                        " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
               " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
               " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
               " OR " + " RENT_CUSTOM.NAME LIKE '%" + search + "%' " +
               ")"
              );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }
            
            
             where.Append(" AND RENT_CONTRACT.STATUS =  " + Constant.RENT_CONTRACT_STATUS_SHENGXIAO);



             PageDataSet ds = dal.GetListByPage(where.ToString(), " STATUS ASC ,  STOP_DATE DESC ", CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];
            this.userControlCommonListPage1.Inits(ds.AllRecord, this.CURRENT_PAGE);


        }

        int XiaoQuSeqid = 0;
        int DongSeqid = 0;


        private void searchClick()
        {
            refreshData(CURRENT_PAGE);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            String CONTRACT_SEQID = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
            String ROOM_SEQID = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["HOUSE_ROOM_SEQID"].Value.ToString();
            String CUSTOM_SEQID = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["RENT_CUSTOM_SEQID"].Value.ToString();
            selectedOk(Convert.ToInt32(CONTRACT_SEQID), Convert.ToInt32(ROOM_SEQID), Convert.ToInt32(CUSTOM_SEQID));
            this.Close();

        }
        // 1:CUSTOM 2:ROOM
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CONTRACT_SEQID"></param>
        /// <param name="ROOM_SEQID"></param>
        /// <param name="CUSTOM_SEQID"></param>
        public delegate void SelectedOk(int CONTRACT_SEQID , int ROOM_SEQID , int CUSTOM_SEQID);
        public SelectedOk selectedOk;

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            setStatusWithDataGrid(this.dataGridView1);
        }

        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS_INT"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByContractStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }
    }
}
